<?php
/**
 * ============================================================================
 * Copyright (c) 2016-2017 #### All rights reserved.
 * ----------------------------------------------------------------------------
 * 文件名称：WeAppAction.class.php
 * ----------------------------------------------------------------------------
 * 功能描述：########
 * ----------------------------------------------------------------------------
 * 作者:chenbei
 * ----------------------------------------------------------------------------
 * 时间:上午9:30:36
 * ----------------------------------------------------------------------------
 */
class WeappAction extends ApiAction {
    public $weChat;
    public function _initialize() {
        parent::_initialize();
        import("@.Libary.weapp");
        $option['appid'] = C('weapp.appid');
        $option['appsecret'] = C('weapp.appsecret');
        $option['token'] = C('weapp.token');
        $option['encodingaeskey'] = C('weapp.encodingaeskey');
        $option['debug'] = true;
        $this->weChat = new Weapp($option);
    }

    public function wechat_entry() {
        $this->weChat->valid();
        $type = $this->weChat->getRev()->getRevType();
        switch($type) {
            case Weapp::MSGTYPE_TEXT:
                $this->weChat->text("hello, I'm wechat")->reply();
                exit;
                break;
        }
    }

    public function login() {

        $MemberDb = D('Member');
        $MemberTokenDB  = D('MemberToken');

        $ret = $this->weChat->getOauthAccessTokenbycode(I("get.code"));


        if(!$ret){
            echo json_encode(array('result'=>'fail','error_code'=>42032,'error_info'=>'获取用户信息失败'));
            return;
        }
        $memberInfo = $MemberDb->getMember(array('open_id'=>$ret['openid']));

        $data['access_token'] = $ret['session_key'];
        $data['expires'] = time() . $ret['expires_in'];
        $data['refresh_token'] = $ret['session_key'];

        if($memberInfo==null) {
            $data['open_id'] = $ret['openid'];
            $data['nickname'] = I('nick_pre');
            $data['headimgurl'] = I('headimgurl');
            $data['subscribe'] = 0;
            $data['time'] = time();
            $data['disablle'] = 0;
            //用户体验期
            $data['extime'] = strtotime("+7 day");
            //会员状态
            $data['member_state'] = 0;
        }

        if($memberInfo==null) {
            $userId = $MemberDb->data($data)->add();

            $WxuserDb = D('Wxuser');
            $wxuser = array();
            $wxuser['member_id']=$userId;
            $wxuser['vipType']=1;
            $wxuser['likeQty']=0;
            $wxuser['collectionQty']=1;
            $wxuser['vipendDate']=date("Y-m-d",strtotime("+3 month"));
            $wxuser['questionQty']=0;
            $data['wxuid'] = $WxuserDb->data($wxuser)->add();
        } else {
            $MemberDb->where(array('member_id' => $memberInfo['member_id']))->save($data);
            $userId = $memberInfo['member_id'];
            $wxuser=D('Wxuser')->where(array('member_id' => $memberInfo['member_id']))->find();
            $data['wxuid'] =$wxuser['wxUid'];
        }

        $data['result'] = 'ok';
        $data['user_sn'] = $userId;

        $memberToken['member_id'] = $userId;
        $memberToken['token'] = generate_password(48);
        $memberToken['expires'] =  $data['expires'];

        $data['token'] = $memberToken['token'];
        $memberTokenDb = D('MemberToken');

        $memberTokenDb->add($memberToken,array(),true);

        echo json_encode($data);
    }

    public function getWxUser(){

        $wxUid = I('get.wxUid');

        $WxuserDb = D('Wxuser');

        $user = $WxuserDb->where(array('wxUid'=>$wxUid))->find();
        $ret['result'] = 'ok';
        $ret['userInfo'] = $user;
        echo json_encode($ret);

    }

   public function level(){
          $wxuId=I('get.wxuId');
          $leone=D("level")->field('starsum')->where(array('sid'=>1))->find();
          $letwo=D("level")->field('starsum')->where(array('sid'=>2))->find();
          $lethree=D("level")->field('starsum')->where(array('sid'=>3))->find();
          $lefour=D("level")->field('starsum')->where(array('sid'=>4))->find();
          $lefive=D("level")->field('starsum')->where(array('sid'=>5))->find();
          $wxuser=D('Wxuser')->field('likeQty')->where(array('wxUid'=>$wxuId))->find();
            

            $sum =   (int)$wxuser['likeQty'];
            $one  =   (int)$leone['starsum'];
            $two  =  (int)$letwo['starsum'];
            $three =    (int)$lethree['starsum'];
            $four  =  (int)$lefour['starsum'];
            $five  = (int)$lefive['starsum'];
            $starnum=0;$jindu=0;
             $pic1="/images/full-star.png";
             $pic2="/images/no-star.png";
             $pic3="/images/no-star.png";
             $pic4="/images/no-star.png";
             $pic5="/images/no-star.png";

           if ($sum>=$one){
            $starnum=5;$jindu=100;
             $pic1="/images/full-star.png";
             $pic2="/images/full-star.png";
             $pic3="/images/full-star.png";
             $pic4="/images/full-star.png";
             $pic5="/images/full-star.png";
           }
           elseif ($sum>=$two) {
             $starnum=4;$jindu=($sum-$two)/($one-$two)*100;
             $pic1="/images/full-star.png";
             $pic2="/images/full-star.png";
             $pic3="/images/full-star.png";
             $pic4="/images/full-star.png";
             $pic5="/images/no-star.png";
            } 
               elseif ($sum>=$three) {
               $starnum=3;$jindu=($sum-$three)/($two-$three)*100;
                $pic1="/images/full-star.png";
               $pic2="/images/full-star.png";
               $pic3="/images/full-star.png";
              $pic4="/images/no-star.png";
              $pic5="/images/no-star.png";
            } 
               elseif ($sum>=$four) {
               $starnum=2;$jindu=($sum-$four)/($three-$four)*100;
                $pic1="/images/full-star.png";
             $pic2="/images/full-star.png";
             $pic3="/images/no-star.png";
             $pic4="/images/no-star.png";
             $pic5="/images/no-star.png";
            } 
             elseif ($sum>=$five) {
                 $starnum=1;$jindu=($sum-$five)/($four-$five)*100;
             $pic1="/images/full-star.png";
             $pic2="/images/no-star.png";
             $pic3="/images/no-star.png";
             $pic4="/images/no-star.png";
             $pic5="/images/no-star.png";
            } 
            else{
                 $starnum=0;$jindu=$sum/($five-$sum)*100;
               $pic1="/images/no-star.png";
             $pic2="/images/no-star.png";
             $pic3="/images/no-star.png";
             $pic4="/images/no-star.png";
             $pic5="/images/no-star.png";
            }

        $ret["starnum"]=$starnum;
        $ret["jindu"]=sprintf("%.2f", $jindu);
        $ret['wxuser']=$wxuser;
        $ret['leone'] = $leone;
        $ret['letwo'] = $letwo;
        $ret['lethree']= $lethree;
        $ret['lefour'] = $lefour;
        $ret['lefive'] = $lefive;
        $ret['pic1']=$pic1;
        $ret['pic2']=$pic2;
        $ret['pic3']=$pic3;
        $ret['pic4']=$pic4;
        $ret['pic5']=$pic5;

        echo json_encode($ret);

        
         // $memberData = $Member->where(array('member_id'=>$wxuser['member_id']))->find();
         // $memberData['sheng']=I("get.sheng");
         // $memberData['city']=I("get.city");
         // $memberData['area']=I("get.area");
         // $memberData['bir']=I("get.birthday");
         // $Member->data($memberData)->save();->field('likeQty')
         


   }





}
